package controller

import (
	"context"
	v1 "wh-1-backend/api/v1"
	"wh-1-backend/internal/model"
	"wh-1-backend/internal/service"
)

type cCompany struct{}

var Company = cCompany{}

func (c *cCompany) GetCompanyPageList(ctx context.Context, req *v1.GetCompanyPageListReq) (res *v1.GetCompanyPageListRes, err error) {
	total, list, err := service.Company().ListByConditionWithPage(ctx, model.PageInput{
		Page:     req.Page,
		PageSize: req.PageSize,
	}, model.CompanyPageListCondition{Title: req.Title})
	res = &v1.GetCompanyPageListRes{
		Total: total,
		List:  list,
	}
	return
}

func (c *cCompany) CreateCompany(ctx context.Context, req *v1.CreateCompanyReq) (res *v1.Res, err error) {
	err = service.Company().Save(ctx, model.EditCompanyInput(req.EditCompanyReq))
	return
}

func (c *cCompany) ModifyCompany(ctx context.Context, req *v1.ModifyCompanyReq) (res *v1.Res, err error) {
	err = service.Company().Update(ctx, req.Id, model.EditCompanyInput(req.EditCompanyReq))
	return
}

func (c *cCompany) DeleteCompany(ctx context.Context, req *v1.DeleteCompanyReq) (res *v1.Res, err error) {
	err = service.Company().Delete(ctx, req.Id)
	return
}

func (c *cCompany) GetCompanyDetail(ctx context.Context, req *v1.GetCompanyDetailReq) (res *v1.GetCompanyDetailRes, err error) {
	company, err := service.Company().GetById(ctx, req.Id)
	res = &v1.GetCompanyDetailRes{Company: *company}
	return
}

func (c *cCompany) GetCompanyList(ctx context.Context, req *v1.GetCompanyListReq) (res *v1.GetCompanyListRes, err error) {
	list, err := service.Company().List(ctx)
	res = &v1.GetCompanyListRes{
		List: list,
	}
	return
}

func (c *cCompany) CreateCompany1(ctx context.Context, req *v1.CreateCompany1Req) (res *v1.CreateCompany1Res, err error) {
	company, err := service.Company().CheckAdnCreateByTitle(ctx, req.Title)
	res = &v1.CreateCompany1Res{Company: company}
	return
}
